import { message } from 'antd';
import { observable, action, toJS } from 'mobx';
import { ComsStore } from './common';
import * as API from '../../apis/docShare';

export class DocShareStore extends ComsStore {
  @observable shareAddVisible = false;
  @observable conditions = [];
  @observable shareAddEditBtn = {};
  @observable shareBatchOperates = [];
  @observable shareBatchOpParams = {};
  @observable shareBatchTableCols = [];
  @observable shareBatchDatas = [];
  @observable selectedKeys = [];

  @action
  setState = (params = {}) => {
    Object.keys(params).forEach((key) => {
      this[key] = params[key];
    });
    return this;
  }

  canShareAddEdit = (params = {}) => {
    API.canShareAddEdit(params).then((data) => {
      this.shareAddEditBtn = data;
    });
    return this;
  }

  getAddShare = (params = {}) => {
    API.getAddShare(params).then((data) => {
      this.conditions = data.conditions;
    });
    return this;
  }

  doShareAddSubmit = (params = {}) => {
    if (params.objType == 2 && params.subcompany != undefined && params.subcompanyspan != undefined) {
      params.obj_id = params.subcompany;
      params.obj_idspan = params.subcompanyspan;
    }
    if (params.objType == 3 && params.department != undefined && params.departmentspan != undefined) {
      params.obj_id = params.department;
      params.obj_idspan = params.departmentspan;
    }
    API.doShareAddSubmit(params).then((data) => {
      data.status === 1
      ? this.doSearch('DocShare', { docid: params.docid })
      : message.warning(data.msg);
    });
    return this;
  }

  deleteShare = (params = {}) => {
    API.deleteShare(params).then((data) => {
      data.status === 1
      ? this.doSearch('DocShare', { docid: params.docid })
      : message.warning(data.msg);
    });
    return this;
  }

  afterShareSave = (params = {}) => {
    API.afterShareSave(params).then(() => {});
    return this;
  }

  getShareBatchChecks = (params = {}) => {
    API.getShareBatchChecks(params).then((data) => {
      const { operates, tableCols } = data;
      this.shareBatchOperates = operates;
      this.shareBatchTableCols = tableCols;
      const shareBatchOpParams = {};
      operates.forEach((op) => {
        shareBatchOpParams[op.key] = '0';
      });
      this.shareBatchOpParams = shareBatchOpParams;
    });
    return this;
  }

  saveShareBatch = (params = {}, callback) => {
    let ss = eval(params.shareDatas);
    for (let i = 0; i < ss.length; i++) {
      if (ss[i].objType == 2 && ss[i].subcompany != undefined && ss[i].subcompanyspan != undefined) {
        ss[i].obj_id = ss[i].subcompany;
        ss[i].obj_idspan = ss[i].subcompanyspan;
      }
      if (ss[i].objType == 3 && ss[i].department != undefined && ss[i].departmentspan != undefined) {
        ss[i].obj_id = ss[i].department;
        ss[i].obj_idspan = ss[i].departmentspan;
      }
   }
   params.shareDatas = JSON.stringify(ss);
    API.saveShareBatch(params).then((result) => {
      callback(result);
    });
    return this;
  }
}
